﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QL_NOI_TRU
{
    public partial class frmNhapTTBenhNhan : DevExpress.XtraEditors.XtraForm
    {
        public frmNhapTTBenhNhan()
        {
            InitializeComponent();
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            var ob = new obTTBenhNhan();
            ob.MABENHNHAN = tbMaBN.Text;
            ob.HOTEN = tbHoTen.Text;
            ob.NGAYSINH = tbNgaySinh.Text;
            ob.GIOITINH = cbbGioiTinh.Text;
            ob.TUOI = tbTuoi.Text;
            ob.DIACHI = tbDiaChi.Text;

            if (ob.MABENHNHAN != "" && ob.HOTEN != "")
            {
                string a = String.Format("Insert into BENH_NHAN VALUES('{0}',N'{1}','{2}',N'{3}',N'{4}',N'{5}')", ob.MABENHNHAN, ob.HOTEN, ob.NGAYSINH, ob.GIOITINH, ob.TUOI, ob.DIACHI);
                var kn = new Ketnoi(frmDangnhap.stringConn);
                kn.ExcuteNonQuery(a);

                tbMaBN.Text = "";
                tbHoTen.Text= "";
                tbNgaySinh.Text="";
                cbbGioiTinh.SelectedIndex = -1;
                tbTuoi.Text = "";
                tbDiaChi.Text = "";

                frmNhapTTBenhNhan_Load(null, null);
                MessageBox.Show("Đã thêm thành công!");
            }
            else
                MessageBox.Show("vui lòng nhập đầy đủ mã bệnh nhân hoặc họ tên bệnh nhân!");

        }

        private void frmNhapTTBenhNhan_Load(object sender, EventArgs e)
        {
            var kn = new Ketnoi(frmDangnhap.stringConn);
            gridControl1.DataSource = kn.ExcuteQuery("select * from BENH_NHAN");
        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            tbMaBN.Text = "";
            tbHoTen.Text = "";
            tbNgaySinh.Text = "";
            cbbGioiTinh.SelectedIndex = -1;
            tbTuoi.Text = "";
            tbDiaChi.Text = "";
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            try {
                var ob = new obTTBenhNhan();
                ob.MABENHNHAN = this.gridView1.GetRowCellValue(0, "MA_BN").ToString();

                tbMaBN.Text = ob.MABENHNHAN;


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            txtMaBN.Enabled = true;
            txtTenBN.Enabled = true;

            txtTenBN.Enabled = false;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            txtMaBN.Enabled = true;
            txtTenBN.Enabled = true;

            txtMaBN.Enabled = false;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                string a = String.Format("select * from BENH_NHAN where MA_BN = '{0}'", txtMaBN.Text);
                var kn = new Ketnoi(frmDangnhap.stringConn);
                gridControl1.DataSource = kn.ExcuteQuery(a);
            }
            else if (radioButton2.Checked ==true)
            {
                var kn = new Ketnoi(frmDangnhap.stringConn);
                gridControl1.DataSource = kn.ExcuteQuery(String.Format("select * from BENH_NHAN where HOTEN ='{0}'", txtTenBN.Text));
            }
        }
    }
}